package com.CmJava.crowd.entity;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Md5Util {

    /**
     *
     * @param source 需要加密的字符串
     * @return 加密后的字符串
     * @throws CrowdException
     */
    public static String md5(String source) {
        if(source==null || source.length()==0){
            throw new CrowdException(CrowdException.EMPTY_EXCEPTION);
        }
        try {
            //加密算法名
            String algorithm="md5";
            MessageDigest messageDigest = MessageDigest.getInstance(algorithm);
            byte[] sourceBytes = source.getBytes();
            //进行加密
            byte[] digest = messageDigest.digest(sourceBytes);
            //signum:1为正数,-1为负数,0为0
            int signum=1;
            // 转成md5数字串
            BigInteger bigInteger = new BigInteger(signum, digest);
            // radix 进制数
            int radix=16;
            // 转成16进制的字符串
            String encode = bigInteger.toString(radix);
            return encode;
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }

        return null;
    }
}
